﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class wait_send : System.Web.UI.Page
    {
        SendRegisterBUS srBUS = null;
        SendRegisterDetailBUS srdBus = null;
        MailConfigBUS mailConfigBus = null;
        MailGroupBUS mailGroupBus = null;
        SendContentBUS sendContentBus = null;
        DetailGroupBUS dsgBus = null;
        CategoryBUS cateBUs = null;
        static UserLoginDTO userLogin;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    
                }
                catch (Exception ex)
                {
                    pnError.Visible = true;
                    lblError.Text = ex.Message;
                }

            }

        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            return null;
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                SendRegisterBUS srBUS = new SendRegisterBUS();
                int sendRegisterId = id;
                //DataTable T = srBUS.GetByID(sendRegisterId);
                //if (T != null && T.Rows.Count > 0)
                //{
                //    if (Convert.ToBoolean(T.Rows[0]["Status"] + ""))
                //    {
                //        throw new Exception("Broadcasting này đã được gửi. Bạn không thể xóa!");
                //    }
                //}
                ConnectionData.OpenMyConnection();
                srBUS.tblSendRegister_Delete(sendRegisterId);
                ConnectionData.CloseMyConnection();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<SendRegisterSearchResult> Search(string from, string to)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            //string name = "", phone = "h", email = ""; int group = -1;
            SendRegisterBUS ctBUS = new SendRegisterBUS();
            DataTable customerBySelect = ctBUS.SearchById(userLogin.UserId, from, to);
            List<SendRegisterSearchResult> result = new List<SendRegisterSearchResult>();
            SendRegisterSearchResult item = new SendRegisterSearchResult();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                item = new SendRegisterSearchResult();
                item.Id = Convert.ToInt32(dr["Id"] + "");
                item.TotalSend = Convert.ToDecimal(dr["TotalSend"] + "");
                item.Sent = Convert.ToDecimal(dr["Sent"] + "");
                item.Name = dr["Name"] + "";
                item.GroupName = dr["GroupName"] + "";
                item.Subject = dr["Subject"] + "";
                if (dr["StartDate"] + "" != "")
                    item.StartDate = Convert.ToDateTime(dr["StartDate"] + "");
                if (dr["EndDate"] + "" != "")
                    item.EndDate = Convert.ToDateTime(dr["EndDate"] + "");
                result.Add(item);
            }

            return result;
        }
    }
    public class SendRegisterSearchResult
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string GroupName { get; set; }
        public string Subject { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal TotalSend { get; set; }
        public decimal Sent { get; set; }
    }

}